import 'package:flutter/material.dart';
import 'login_page.dart';
import 'register_page.dart';
class Attraction {
  final String image;
  final String title;
  final String description;
  final String price;

  Attraction({
    required this.image,
    required this.title,
    required this.description,
    required this.price,
  });
}

class Aim extends StatefulWidget {
  const Aim({super.key});

  @override
  State<Aim> createState() => _AimState();
}

class _AimState extends State<Aim> {
  int _selectedIndex = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页"),
          BottomNavigationBarItem(icon: Icon(Icons.event), label: "活动"),
          BottomNavigationBarItem(icon: Icon(Icons.assignment), label: "订单"),
          BottomNavigationBarItem(icon: Icon(Icons.person), label: "我的"),
        ],
        currentIndex: _selectedIndex,
        onTap: (index) {
          setState(() {
            _selectedIndex = index;
          });
        },
        selectedItemColor: Colors.orange,
        unselectedItemColor: Colors.grey,
        backgroundColor: Colors.white,
      ),
      body: IndexedStack(
        index: _selectedIndex,
        children: [
          HomePage(),
          Activity()
        ],
      ),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _selectedIndex = 0;
  final TextEditingController _searchController = TextEditingController();

  String _selectedLayout = '竖排';
  String _selectedSort = '最新上架';
  bool _showLayoutOptions = false;

  // 模拟JSON文件
  final List<Attraction> _allAttractions = [
    Attraction(
      image: "assets/images/大连三寰牧场.png",
      title: "大连三寰牧场",
      description: "致那些远去的记忆和绿色",
      price: "¥185.0起",
    ),
    Attraction(
      image: "assets/images/大连将军石温泉酒店.png",
      title: "大连将军石温泉酒店",
      description: "钱塘往事|致那些远去的记...",
      price: "¥285.0起",
    ),
    Attraction(
      image: "assets/images/大连森林动物园.png",
      title: "大连森林动物园",
      description: "致那些远去的记忆和绿色",
      price: "¥120.0起",
    ),
    Attraction(
      image: "assets/images/金石滩旅游度假区.png",
      title: "金石滩旅游度假区",
      description: "金沙滩旅游度假区",
      price: "¥150.0起",
    ),
    Attraction(
      image: "assets/images/大连现代博物馆.png",
      title: "大连现代博物馆",
      description: "大连现代博物馆(好再来嘿)",
      price: "¥80.0起",
    ),
  ];

  List<Attraction> _filteredAttractions = [];

  @override
  void initState() {
    super.initState();
    _filteredAttractions = _allAttractions;
  }

  void _onSearchPressed() {
    _performSearch();
  }
  void _selectSort(String sort) {
    setState(() {
      _selectedSort = sort;

      // 复制一份原始列表
      _filteredAttractions = List.from(_allAttractions);

      // 根据选择的排序方式处理列表
      if (sort == '人气最高' || sort == '用户评分') {
        // 随机打乱列表顺序
        _filteredAttractions.shuffle();
      }
      // 最新上架保持原顺序
    });
  }
  void _performSearch() {
    final query = _searchController.text.toLowerCase();

    setState(() {
      if (query.isEmpty) {
        _filteredAttractions = _allAttractions;
      } else {
        _filteredAttractions = _allAttractions.where((attraction) {
          return attraction.title.toLowerCase().contains(query) ||
              attraction.description.toLowerCase().contains(query);
        }).toList();
      }
    });

    FocusScope.of(context).unfocus();
  }

  void _toggleLayoutOptions() {
    setState(() {
      _showLayoutOptions = !_showLayoutOptions;
    });
  }

  void _selectLayout(String layout) {
    setState(() {
      _selectedLayout = layout;
      _showLayoutOptions = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Padding(
          padding: EdgeInsets.all(15),
          child: Column(
            children: [
              // 搜索框部分（老铁你要的搜索框在这里！）
              Container(
                margin: EdgeInsets.symmetric(horizontal: 16.0),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(25.0),
                  border: Border.all(color: Colors.pinkAccent, width: 1.0),
                ),
                child: Row(
                  children: [
                    IconButton(
                      icon: Icon(Icons.search),
                      onPressed: () {},
                    ),
                    Expanded(
                      child: TextField(
                        controller: _searchController,
                        decoration: InputDecoration(
                          hintText: '搜索景点',
                          border: InputBorder.none,
                        ),
                        textInputAction: TextInputAction.search,
                        onSubmitted: (value) => _performSearch(),
                      ),
                    ),
                    ElevatedButton(
                      onPressed: _onSearchPressed,
                      style: ElevatedButton.styleFrom(
                        primary: Colors.orange,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(25.0),
                        ),
                        padding: EdgeInsets.symmetric(
                            horizontal: 24.0, vertical: 12.0),
                      ),
                      child: Text('搜索', style: TextStyle(fontSize: 18.0)),
                    ),
                  ],
                ),
              ),
              SizedBox(height: 10),
              // 布局和排序选项
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  // 布局选择下拉框
                  GestureDetector(
                    onTap: _toggleLayoutOptions,
                    child: Container(
                      padding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
                      decoration: BoxDecoration(
                        border: Border.all(color: Colors.grey),
                        borderRadius: BorderRadius.circular(4),
                      ),
                      child: Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Text(_selectedLayout),
                          Icon(Icons.arrow_drop_down),
                        ],
                      ),
                    ),
                  ),

                  // 排序下拉框
                  DropdownButton<String>(
                    value: _selectedSort,
                    onChanged: (String? newValue) {
                      if (newValue != null) {
                        _selectSort(newValue);
                      }
                    },
                    items: <String>['最新上架', '人气最高', '用户评分']
                        .map<DropdownMenuItem<String>>((String value) {
                      return DropdownMenuItem<String>(
                        value: value,
                        child: Text(value),
                      );
                    }).toList(),
                  ),
                ],
              ),
              // 布局选项下拉菜单
              if (_showLayoutOptions)
                Container(
                  margin: EdgeInsets.only(top: 4),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(4),
                    boxShadow: [
                      BoxShadow(
                        color: Colors.black12,
                        blurRadius: 4,
                        offset: Offset(0, 2),
                      ),
                    ],
                  ),
                  child: Column(
                    children: [
                      _buildLayoutOption('竖排', Icons.view_agenda),
                      Divider(height: 1),
                      _buildLayoutOption('横排', Icons.grid_view),
                    ],
                  ),
                ),
              Divider(),
              SizedBox(height: 10),
              // 景点列表
              _filteredAttractions.isEmpty
                  ? Center(
                child: Padding(
                  padding: EdgeInsets.all(20),
                  child: Text("没有找到相关景点", style: TextStyle(fontSize: 18)),
                ),
              )
                  : _selectedLayout == '竖排'
                  ? _buildVerticalLayout()
                  : _buildHorizontalLayout(),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildLayoutOption(String layout, IconData icon) {
    return ListTile(
      leading: Icon(icon),
      title: Text(layout),
      trailing: _selectedLayout == layout
          ? Icon(Icons.check, color: Colors.orange)
          : null,
      onTap: () => _selectLayout(layout),
    );
  }

  Widget _buildVerticalLayout() {
    return Column(
      children: _filteredAttractions.map((attraction) => Padding(
        padding: EdgeInsets.only(bottom: 16),
        child: _buildAttractionCard(attraction),
      )).toList(),
    );
  }

  Widget _buildHorizontalLayout() {
    return GridView.count(
      shrinkWrap: true,
      physics: NeverScrollableScrollPhysics(),
      crossAxisCount: 2,
      childAspectRatio: 0.7,
      mainAxisSpacing: 16,
      crossAxisSpacing: 16,
      children: _filteredAttractions.map((attraction) =>
          _buildAttractionCard(attraction),
      ).toList(),
    );
  }

  Widget _buildAttractionCard(Attraction attraction) {
    return Container(
      decoration: BoxDecoration(
        border: Border.all(color: Colors.grey),
        borderRadius: BorderRadius.circular(22),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          ClipRRect(
            borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
            child: Image.asset(
              attraction.image,
              height: 100,
              width: double.infinity,
              fit: BoxFit.cover,
            ),
          ),
          Padding(
            padding: EdgeInsets.all(12),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(  // 这里开始
                  attraction.title,
                  style: TextStyle(fontSize: 17, fontWeight: FontWeight.bold),
                ),  // 这里缺少了逗号
                SizedBox(height: 4),  // 这里缺少了逗号
                Text(
                  attraction.description,
                  style: TextStyle(color: Colors.grey, fontSize: 14),
                ),
                SizedBox(height: 8),
                Row(
                  children: [
                    Container(
                      padding: EdgeInsets.symmetric(horizontal: 6, vertical: 2),
                      decoration: BoxDecoration(
                        color: Colors.orangeAccent,
                        borderRadius: BorderRadius.circular(4),
                      ),
                      child: Text("5A景区", style: TextStyle(fontSize: 12)),
                    ),
                    SizedBox(width: 8),
                    Container(
                      padding: EdgeInsets.symmetric(horizontal: 6, vertical: 2),
                      decoration: BoxDecoration(
                        border: Border.all(color: Colors.grey),
                        borderRadius: BorderRadius.circular(4),
                      ),
                      child: Text("限制1.32m以上", style: TextStyle(fontSize: 12)),
                    ),
                  ],
                ),
                SizedBox(height: 8),
                Text(
                  attraction.price,
                  style: TextStyle(
                      fontSize: 18,
                      color: Colors.orange,
                      fontWeight: FontWeight.bold),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

}

class Activity extends StatelessWidget {
  const Activity({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Text("huodong"),
    );
  }
}
